package com.controller.merchant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.dto.merchant.OffersDTO;
import com.model.dao.OffersTable;
import com.model.dao.OffersTableDAO;

@ManagedBean(name = "offersController")
@SessionScoped
public class OffersController implements Serializable{

	private static final long serialVersionUID = -6027207385371118770L;
	
	public void saveOrUpdateOffer(OffersDTO offersDTO) throws Exception{
		OffersTableDAO dao = new OffersTableDAO();
		OffersTable table=new OffersTable();
		table.setOfferImage(offersDTO.getOfferImage());
		table.setOfferName(offersDTO.getOfferName());
		table.setActive(offersDTO.isActive() ? "Y" :"N");
		dao.persist(table);
	}
	
	public List<OffersDTO> findAll() throws Exception{
		OffersTableDAO dao = new OffersTableDAO();
		List<OffersDTO> offersList=new ArrayList<OffersDTO>();
		List<OffersTable> list=dao.findByAll();
		for(OffersTable table:list){
			OffersDTO dto=new OffersDTO();
			dto.setActive(true);
			dto.setIdoffersTable(table.getIdoffersTable());
			dto.setOfferImage(table.getOfferImage());
			dto.setOfferName(table.getOfferName());
			offersList.add(dto);
		}
		return offersList;
	}
	public void updateOffer(OffersDTO offersDTO) throws Exception{
		OffersTableDAO dao = new OffersTableDAO();
		OffersTable table=new OffersTable();
		table.setIdoffersTable(offersDTO.getIdoffersTable());
		table.setOfferImage(offersDTO.getOfferImage());
		table.setOfferName(offersDTO.getOfferName());
		table.setActive(offersDTO.isActive() ? "Y" :"N");
		dao.merge(table);
	}
}
